package com.maimang.platform.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname WebSocketConfig
 * @description 开启WebSocket支持, 启用STOMP协议来传输基于代理（message broker)的消息
 * @date 2019-04-23 19:11
 * @email jintaozhao@qq.com
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		registry.addEndpoint("platform-socket")
				.setAllowedOrigins("*")
				.withSockJS();
	}

	@Override
	public void configureMessageBroker(MessageBrokerRegistry registry) {
		registry.enableSimpleBroker("/queue", "/topic");
		// 设置服务器广播消息的基础路径
		registry.enableSimpleBroker("/queue", "/topic");
		// 设置客户端订阅消息的基础路径
		// 表示客户单向服务器端发送时的主题上面需要加"/app"作为前缀。
		registry.setApplicationDestinationPrefixes("/app");
		// 表示服务端给客户端指定用户发送一对一的主题，前缀是"/user"
		registry.setUserDestinationPrefix("/user");
	}
}
